<template>
  <Spin :spinning="loading" tip="Loading...">
    <div>
      <!--旧件选择列表 -->
      <TopFilter ref="TopFilters" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="100"
        @filterChange="changeHandle"></TopFilter>
      <!--  <BreakSpace label="旧件选择列表" :containerStyle="{ marginBottom: '10px' }" /> -->
      <FilterTable ref="table" :columns="columns" :params="fetchParams" :dataSource="dataList" :datakey="'records'"
        :fetchapi="fetchApi" :onColumnsChange="columns => (this.columns = columns)" :onRowSelectChange="selectChange"
        selectionType="multiple" :height="'auto'">
      </FilterTable>

      <div style="margin-bottom: 1rem;"></div>
      <div style="padding-top: 3rem;"></div>
      <div :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }">
        <multiuse-button type="primary" :click="saveDialog.bind()">添 加</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </div>
  </Spin>
</template>
<script>
//公共方法
import { getPartType } from '@/api/dictionary/index.js';
import { getAllStockInfo, getListByExampleWhere } from '@/api/usedPartsReturnManage/usedPartsReturn_OEM/usedPartsOutOfStock';
import { dictionary } from '@/utils/dictMixin';
import { getAppId } from '@/assets/js/auth';
export default {
  props: ['rowData', 'rowType', 'dataList'],
  mixins: [dictionary],
  components: {
  },
  data() {
    this.searchstore();
    return {
      loading: false,
      dataList: [],
      visible: false,
      visibleDrawer: false,
      selectDelearFlag: false,
      collapse: true,
      infoId: null,
      fetchApi: getAllStockInfo,
      columns: this.creatHeader(),
      fetchParams: { dealerName: this.rowData, partStatus: this.rowType }, //ipa 接口
      selectedRows: [], //选中服务站
      topFilterInit: this.createFormList(),
      saveData: null
    };
  },
  created() {
    /*  if (getAppId() == 'arcfox') {
       this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543',[55431001])
     }else{
       this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543')
     } */
    /*  this.chuqi(); */
  },
  methods: {
    //下拉仓库
    async searchstore() {
      try {
        const res = await getListByExampleWhere();
        this.topFilterInit.find(item => item.fieldName === 'whName').itemList = res.data.map(x => ({ value: x.WH_NAME, text: x.WH_NAME }));
      } catch (err) {
        console.log(err);
      }
    },

    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val, { xhrAbort: false });
      console.log(val);
      /*  this.fetchParams = Object.assign({ ownerCode: this.ownerCode }, this.fetchParams, val, { xhrAbort: false });
       console.log(val); */
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //删除
    deleteHandle(row) {
      this.$refs.table.$refs.pageTable.EXECUTE_DELETE([row]);
    },
    //取消操作
    closeDialog() {
      this.$emit('close');
    },
    //保存
    saveDialog() {
     console.log('11%%%%%%%%%%%%%%%%%%%',this.dataList );
     let arrList = [];
     for (let i = 0;i < this.dataList.length;i++){
      // this.dataList[0]this.selectedRows[i]
      arrList.push(this.dataList[i].partCode);
     }
     // console.log('%%%%%%%%%%%%%%%%%%%',this.selectedRows[0].partStatus);
      if (this.selectedRows.length) {
        let flag = false;
        for(let i = 0 ; i < this.selectedRows.length; i++){
          if (arrList.indexOf(this.selectedRows[i].partCode) > -1) {
            this.$notify.error({ title: '提示信息', message: '旧件代码['+(this.selectedRows[i].partCode)+']已存在，请重新选择' });
            flag = true;
            return false;
          }
        }
        if (flag) {
          return ;
        }
      //  if(this.rowType == this.selectedRows[0].partStatus  ||this.rowType == null){
        //判断旧件代码不同
        let obj = {};let rtnData = [];
        for(let i = 0 ; i < this.selectedRows.length; i++){
          if (obj && obj[this.selectedRows[i].partCode]) {
            // 表示重复了
            // obj[this.selectedRows[i].supCode] += 1;
            this.$notify.error({ title: '提示信息', message: '旧件代码['+(this.selectedRows[i].partCode)+'],不能重复' });
            flag = true;
            return false;
          } else {
            obj[this.selectedRows[i].partCode] = 1;
            let rtnObj = Object.assign({}, this.selectedRows[i], {outStatus: this.rowType,stockCount:this.selectedRows[i].inCount});
          console.log("this.selectedRows[i] === "+ JSON.stringify(rtnObj));
            rtnData.push(rtnObj);
          }

        /*   this.selectedRows[i].rdId = this.selectedRows[i].dtlId;
          delete this.selectedRows[i].dtlId; */
        }
        //this.loading = true;
        if (!flag) {
          this.$emit('save', rtnData);
        }

      /*   else{
          this.$notify.error({ title: '提示信息', message: '库存类型需要保持一致，请选择相同的库存类型' });
          return;

        } */

      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'whName',
          placeholder: '请选择仓库...',
          itemList: [],
          initialValue: '',
        },
        {
          type: 'SELECT',
          label: '库存类型',
          fieldName: 'partStatus',
          placeholder: '请选择入库类型...',
          initialValue: this.rowType,
          readonly: true,
          disabled: true,
          itemList: [{ text: '合格库', value: 1 }, { text: '不合格库', value: 0 }]
        },
        {
          type: 'INPUT',
          label: '服务中心简称',
          fieldName: 'dealerName',
          placeholder: '请输入服务中心简称...',
          initialValue: this.rowData,
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '旧件代码',
          fieldName: 'partCode',
          placeholder: '请选择...',
        },
        {
          type: 'INPUT',
          label: '旧件名称',
          fieldName: 'partName',
          placeholder: '请输入旧件名称...',
        },

      ];
    },
    //旧件列表
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        /*  {title: 'dtlId', dataIndex: 'dtlId', sorter: true, hidden: true},
         {title: '索赔单号', dataIndex: 'cmsClaimNo', sorter: true},
         {title: 'VIN', dataIndex: 'vin', sorter: true, width: 150},
         {title: '索赔类型',
          dataIndex: 'cmsClaimType',
          sorter: true,
          dictItems: this.createDictList('5510'),
          /* render: props => {
             return <span>{this.createDictText(props.row.cmsClaimType, '5510')}</span>;
           }
         }, */
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        /*    {title: '入库时间', dataIndex: 'createdAt', sorter: true, width: 140},
           {title: '入库数量', dataIndex: 'inCount', sorter: true}, */
        { title: '库存数量', dataIndex: 'inCount', sorter: true },
        /* {title: 'dtlId', dataIndex: 'dtlId', sorter: true, hidden: true},
        {title: '索赔单号', dataIndex: 'cmsClaimNo', sorter: true},
        { title: '车辆属性', dataIndex: 'ifComm', sorter: true,filterItems: this.createDictList('5543'),
          dictItems: this.createDictList('5543'),
          render: props => {
            return <span>{this.createDictText(props.row.ifComm, '5543')}</span>;
          } },
        {title: '保修单号', dataIndex: 'roNo', sorter: true},
        {title: '供应商', dataIndex: 'supName', sorter: true},
        {title: '旧件代码', dataIndex: 'partCode', sorter: true},
        {title: '旧件名称', dataIndex: 'partName', sorter: true},
        {title: '库存数量', dataIndex: 'partCount', sorter: true},
        {
          title: '出库数量',
          dataIndex: 'outCount',
          sorter: true,
          minWidth: 120,
          editable: true,
          defaultEditable: true,
          showOverflowTooltip: true,
          editType: 'input'
        },
        {title: 'VIN', dataIndex: 'vin', sorter: true, width: 150},
        {title: '旧件状态', dataIndex: 'statusStr', sorter: true},
        {title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true},
        {title: '入库日期', dataIndex: 'inStockDate', sorter: true, width: 140} */
      ];
    }

  }
};
</script>
<style lang="less" scoped>

</style>
